package model;

import com.alibaba.fastjson.JSON;

/**
 * 客户端请求响应体消息model
 */
public class ApiResultModel<T> {

    public final static int SUCCESS_CODE = 1;
    public final static int FAIL_CODE = 0;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getResult() {
        return result;
    }

    public void setResult(T result) {
        this.result = result;
    }

    private int code;
    private String message;
    private T result;

    public ApiResultModel(int code, String message, T result) {
        this.code = code;
        this.message = message;
        this.result = result;
    }

    public String json() {
        return JSON.toJSONString(this);
    }

    public static ApiResultModel successWithMessage(String message) {
        return new ApiResultModel<>(SUCCESS_CODE, message, null);
    }

    public static ApiResultModel failedWithMessage(String message) {
        return new ApiResultModel<>(FAIL_CODE, message, null);
    }

    public static ApiResultModel successWithObject(Object object) { return new ApiResultModel<>(SUCCESS_CODE, "success", object);}

}
